package com.uls.web.controller.ycl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.uls.framework.config.MinioProp;
import com.uls.framework.utils.MinioUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.uls.common.annotation.Log;
import com.uls.common.core.controller.BaseController;
import com.uls.common.core.domain.AjaxResult;
import com.uls.common.enums.BusinessType;
import com.uls.ycl.domain.RepairMaterial;
import com.uls.ycl.service.IRepairMaterialService;
import com.uls.common.utils.poi.ExcelUtil;
import com.uls.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 材料Controller
 * 
 * @author ycl
 * @date 2024-11-09
 */
@RestController
@RequestMapping("/ycl/material")
public class RepairMaterialController extends BaseController
{
    @Autowired
    private IRepairMaterialService repairMaterialService;

    @Autowired
    private MinioUtils minioUtils;
    @Autowired
    private MinioProp minioProp;

    /**
     * 查询材料列表
     */
    @PreAuthorize("@ss.hasPermi('ycl:material:list')")
    @GetMapping("/list")
    public TableDataInfo list(RepairMaterial repairMaterial)
    {
        startPage();
        List<RepairMaterial> list = repairMaterialService.selectRepairMaterialList(repairMaterial);
        return getDataTable(list);
    }

    /**
     * 导出材料列表
     */
    @PreAuthorize("@ss.hasPermi('ycl:material:export')")
    @Log(title = "材料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RepairMaterial repairMaterial)
    {
        List<RepairMaterial> list = repairMaterialService.selectRepairMaterialList(repairMaterial);
        ExcelUtil<RepairMaterial> util = new ExcelUtil<RepairMaterial>(RepairMaterial.class);
        util.exportExcel(response, list, "材料数据");
    }

    /**
     * 获取材料详细信息
     */
    @PreAuthorize("@ss.hasPermi('ycl:material:query')")
    @GetMapping(value = "/{materialId}")
    public AjaxResult getInfo(@PathVariable("materialId") Long materialId)
    {
        return success(repairMaterialService.selectRepairMaterialByMaterialId(materialId));
    }

    /**
     * 新增材料
     */
    @PreAuthorize("@ss.hasPermi('ycl:material:add')")
    @Log(title = "材料", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RepairMaterial repairMaterial) {
        if (repairMaterial == null) {
            return AjaxResult.error("维修材料信息不能为空");
        }

        repairMaterial.setCreatedAt(new Date());

        BigDecimal total = Optional.ofNullable(repairMaterial.getTotal())
                .map(BigDecimal::new)
                .orElse(BigDecimal.ZERO);
        BigDecimal price = Optional.ofNullable(repairMaterial.getPrice())
                .orElse(BigDecimal.ZERO);

        // 检查材料数量是否为0
        if (total.compareTo(BigDecimal.ZERO) == 0) {
            repairMaterial.setTotalPrice(BigDecimal.ZERO);
        } else {
            repairMaterial.setTotalPrice(total.multiply(price));
        }

        return toAjax(repairMaterialService.insertRepairMaterial(repairMaterial));
    }


    /**
     * 修改材料
     */
    @PreAuthorize("@ss.hasPermi('ycl:material:edit')")
    @Log(title = "材料", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RepairMaterial repairMaterial)
    {

        return toAjax(repairMaterialService.updateRepairMaterial(repairMaterial));
    }

    /**
     * 删除材料
     */
    @PreAuthorize("@ss.hasPermi('ycl:material:remove')")
    @Log(title = "材料", businessType = BusinessType.DELETE)
	@DeleteMapping("/{materialIds}")
    public AjaxResult remove(@PathVariable Long[] materialIds)
    {
        return toAjax(repairMaterialService.deleteRepairMaterialByMaterialIds(materialIds));
    }

    /**
     * 上传
     *
     * @param file
     * @param request
     * @return
     */
    @PostMapping("/uploadMinio")
    public String uploadMinio(@RequestParam(name = "file", required = false) MultipartFile file, HttpServletRequest request) {
        JSONObject res = null;
        try {
            res = minioUtils.uploadFile(file, minioProp.getBucket());
        } catch (Exception e) {
            e.printStackTrace();
            res.put("code", 0);
            res.put("msg", "上传失败");
        }
        return res.toJSONString();
    }
    @PreAuthorize("@ss.hasPermi('ycl:RepairMaterial:down')")
    @PostMapping("/imageDown")
    public  void imageDown(String fileName, HttpServletResponse response){
        minioUtils.download(minioProp.getBucket(),fileName,response);
    }


    // 获取所有材料
    @GetMapping("/getAllMaterials")
    public AjaxResult getAllMaterials(){
    	RepairMaterial material = new RepairMaterial();
        List<RepairMaterial> list = repairMaterialService.selectRepairMaterialList(material);
        return success(list);
    }

}
